﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
using System.Windows.Controls;
using KZ400B.Data;

namespace KZ400B.Controls
{

    public class MsgStateItem
    {
        public MsgStateItem(
            Label control
            ,string state
          )
        {
            m_control = control;
            m_state = state;
        }

        Label m_control;

        public Label Control
        {
            get { return m_control; }
           internal set { m_control = value; }
        }
        string m_state;

        public string State
        {
            get { return m_state; }
           // set { m_state = value; }
        }
    }

    public class StateMsgManager
    {
        static bool m_isR = false;
        static object m_isRlk = new object();

        public static bool IsRunning
        {
            get { lock (m_isRlk) { return m_isR; } }
            set { lock (m_isRlk) { m_isR = value; } }
        }

        static Thread m_tR = null;
        public static void Start()
        {
            //if (IsRunning) return;
            //if (m_tR == null)
            //{
            //    IsRunning = true;
            //    m_tR = new Thread(new ThreadStart(proc));
            //    m_tR.Start();
            //}
        }

        public static void Stop()
        {
            //IsRunning = false;
            //try
            //{
            //    m_tR.Join();
            //    m_tR = null;
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.Message);
            //}

        }

        static Queue m_addQueue = Queue.Synchronized(new Queue());
        static Label m_control = null;

        public static void addOneState(string msg)
        { 
            addOneState(new MsgStateItem(m_control,msg));
        }
        //public static void addOneState(MsgStateItem state)
        //{
        //    if (!IsRunning) return;
        //    lock (m_addQueue.SyncRoot)
        //    {
        //        if (m_addQueue.Count < 10000 && state != null)
        //        {
        //            if (state.Control != null)
        //            {
        //                m_control = state.Control;
        //            }
        //            m_addQueue.Enqueue(state);
        //        }
        //    }
        //}

        public static void addOneState(MsgStateItem state)
        {
            AxleStateBarMsg.Instance.AxleState = state.State;
        }


        static void proc()
        {
            while (IsRunning)
            {
                MsgStateItem state = null;
                lock (m_addQueue.SyncRoot)
                {
                    if (m_addQueue.Count > 0)
                    {
                        state = m_addQueue.Dequeue() as MsgStateItem;
                    }
                }
                if (state != null)
                {
                    doChangeState(state);
                }
                else
                {
                    Thread.Sleep(200);
                }
            }
        }

        static void doChangeState(MsgStateItem itm)
        {
            if (itm.Control != null)
            {
                itm.Control.Dispatcher.BeginInvoke(new Action(delegate() { itm.Control.Content = itm.State; }));
            }
        }

        void chgState(MsgStateItem itm)
        {
            if (itm.Control != null)
            {
                itm.Control.Content = itm.State;
            }
        }
    }
}
